package com.sunxs;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * mybatis plus generator 代码生成器
 */
class generator {
    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/javaee?serverTimezone=Asia/Shanghai", "root", "9120")
                // 全局配置
                .globalConfig((builder) ->
                        builder.author("孙先生")
                                .enableSwagger()
                                .outputDir("D://workplace//mybatis-generator//src//main//java"))
                // 包配置
                .packageConfig((builder) -> builder.parent("com.sunxs").moduleName("sys").pathInfo(Collections.singletonMap(OutputFile.xml, "D://workplace//mybatis-generator//src//main//resources//mappers")))
                // 策略配置
                .strategyConfig((scanner, builder) -> builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all")))
                        //.addTablePrefix(REMOVE_TABLE_PREFIX)
                        .controllerBuilder()
                        .enableRestStyle()
                        .enableHyphenStyle()
                        .entityBuilder()
                        .enableLombok()
                        .addTableFills(new Column("create_time", FieldFill.INSERT))
                        .build())
                /*
                    模板引擎配置，默认 Velocity 可选模板引擎 Beetl 或 Freemarker
                   .templateEngine(new BeetlTemplateEngine())
                   .templateEngine(new FreemarkerTemplateEngine())
                 */
                .templateConfig(config -> {
                    config // 禁用模板	TemplateType.ENTITY
                            //.entity("/templates/entity.java")// 设置实体模板路径(JAVA)	/templates/entity.java
                            //.service("/templates/service.java")
                            //.serviceImpl("/templates/serviceImpl.java")
                            //.mapper("/templates/mapper.java")
                            //.xml("/templates/mapper.xml")
                            .controller("/templates/controller.java")
                            .build();
                })
                .execute();
    }

    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }
}
